<?php
/**
 * Query.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/17- 10:43 上午
 */
namespace common\models\es\dataProvider\copyProducts;

use common\helpers\AmountHelper;
use common\helpers\StringHelper;
use common\models\es\EsCopyProducts;

class Query
{
    /**
     * 查询字段
     * @var array
     */
    protected $fields = [];

    /**
     * 查询条件
     * @var array
     */
    protected $query = [];

    /**
     * 排序方式
     * @var string
     */
    protected $sort = [];


    /**
     * 每页条数
     * @var
     */
    protected $size;


    /**
     * 偏移量
     * @var
     */
    protected $from;


    public function __construct(array $params, array $fields = [], int $page = 1, int $size = 20)
    {
        $page = $page ? $page : 1;
        $size = $size ? $size : 20;

        $offset = max(($page - 1), 0) * $size;

        $this->from = $offset;
        $this->size = $size;
        if(isset($params['sort']) && is_array($params['sort']))
        {
            $this->sort = $params['sort'];
        }else{
            $this->sort = ['id' => ['order' => 'DESC']];
        }


        $this->formatFields($fields);

        $this->formatQuery($params);
    }

    /**
     * 查询字段
     * @param $fields
     */
    protected function formatFields($fields)
    {
        $es = new EsCopyProducts();
        $attributes = $es->attributes();
        $search_fields = [];
        foreach ($fields as $field) {
            if (in_array($field, $attributes)) {
                $search_fields[$field] = $field;
            }
        }
        $this->fields = $search_fields ? array_values($search_fields) : $attributes;
    }

    /**
     * 查询条件
     * @param $params
     */
    protected function formatQuery($params)
    {
        $query = [];

        if (isset($params['status']) && !empty($params['status'])) {
            $query['status'] = is_array($params['status']) ? $params['status'] : [$params['status']];
        }

        //批次id
        if (isset($params['batch_no']) && !empty($params['batch_no'])) {
            $query['batch_no'] = intval($params['batch_no']);
        }
        //会员id
        if (isset($params['uid']) && !empty($params['uid'])) {
            $query['uid'] = intval($params['uid']);
        }

        //店铺id
        if (isset($params['mall_id']) && !empty($params['mall_id'])) {
            $query['mall_id'] = intval($params['mall_id']);
        }


        //是否删除
        if (isset($params['is_delete'])) {
            $query['is_delete'] = intval($params['is_delete']);
        }

        //商品id
        if (isset($params['num_iid']) && !empty($params['num_iid'])) {
            $cutNumIIds = StringHelper::cutByString($params['num_iid']);

            $query['num_iid'] = $cutNumIIds;
        }

        //平台id
        if (isset($params['platform']) && !empty($params['platform'])) {
            $query['platform'] = intval($params['platform']);
        }
        //商品状态
        if (isset($params['product_status']) && !empty($params['product_status'])) {
            $query['product_status'] = intval($params['product_status']);
        }

        //商品标题
        if (isset($params['title']) && !empty($params['title'])) {
            $query['title'] = trim($params['title']);
        }

        //开始时间
        if (isset($params['start_at']) && !empty($params['start_at'])) {
            $query['start_at'] = strtotime($params['start_at']);
        }
        //结束时间
        if (isset($params['end_at']) && !empty($params['end_at'])) {
            $query['end_at'] = strtotime($params['end_at']);
        }

        $this->query = $query;
    }



    /**
     * 获取检索的索引名称
     * @return string
     */
    protected function getIndexName(): string
    {
        return EsCopyProducts::INDEX;
    }
}
